package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Observable;
import java.util.Vector;

import view.ModifyEvent;

public class Evento extends Observable{

	static int lastEvnIdDb=0;
	private int evn_id = 0;
	private String nome = "";
	private String descr = "";
	private String luogo = "";
	private String day = "00/00/0000";
	private String time = "00:00";
	private int level = 0;
	private String type = "Altro";
	private int acc_id = 0;
	public String dd,mm,yy;
	public String ora = "00";
	public String min = "00";
	private Date data = GregorianCalendar.getInstance().getTime();
	private Vector<Account> invitati = new Vector<Account>();

	public Evento() {
		//setEvn_id(getLastEvn_IdOnDb());
		this.evn_id = get_LastEvnIDFromDB()+1;
		Evento.lastEvnIdDb = this.evn_id+1;
	}

	public Evento(int evn_id,String nome,String descr,String luogo,String day,String time,String type,int level,int acc_id){
		setEvn_id(evn_id);
		setNome(nome);
		setDescr(descr);
		setLuogo(luogo);
		setDay(day);
		setTime(time);
		setType(type);
		setLevel(level);
		setAcc_id(acc_id);
		if (this.evn_id>Evento.lastEvnIdDb)
			Evento.lastEvnIdDb = this.evn_id;
	}

	public int get_LastEvnIDFromDB() {
		ResultSet result = Database.getQueryResult("SELECT evn_id FROM evento");
		int id=0;
		try {
			while(result.next())
				id=result.getInt("evn_id");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Evento.lastEvnIdDb = id+1;
		return id+1;
	}

	public int getEvn_id() {
		//evn_id = get_LastEvnIDFromDB();
		return evn_id;
	}

	public void setEvn_id(int evn_id) {
		this.evn_id = evn_id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getDescr() {
		return descr;
	}

	public void setDescr(String descr) {
		this.descr = descr;
	}

	public String getLuogo() {
		return luogo;
	}

	public void setLuogo(String luogo) {
		this.luogo = luogo;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getType() {
		return type;
	}

	/*	public int getIntType(){
		int tipo=0;
		switch (this.type){
		case"Evento": tipo=0;break;
		case"Lavoro": tipo=1;break;
		case"Compleanno": tipo=2;break;
		case"Festa": tipo=3;break;
		case"Vacanza": tipo=4;break;
		}
		return tipo;
	}
	 */

	public void setType(String type) {
		this.type = type;
	}

	public int getAcc_id() {
		return acc_id;
	}

	public void setAcc_id(int acc_id) {
		this.acc_id = acc_id;
	}
	public String getDay() {
		return day;
	}
	public void setDay(String day) {
		this.day = day;
		setDate();
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
		this.ora = ""+this.time.charAt(0)+this.time.charAt(1);
		this.min = ""+this.time.charAt(3)+this.time.charAt(4);
	}

	public void setTime(int hh, int mm){
		if (hh<10){
			this.ora = "0"+hh;
			this.time= "0"+hh;
		}
		else{
			this.ora = ""+hh;
			this.time=""+hh;
		}
		if (mm<10){
			this.min = "0"+mm;
			this.time+=":0"+mm;
		}
		else{
			this.min = ""+mm;
			this.time+=":"+mm;
		}
	}

	public void setDate() {
		this.dd = ""+this.day.charAt(0);
		this.dd += this.day.charAt(1);
		this.mm = ""+this.day.charAt(3);
		this.mm += this.day.charAt(4);
		for (int i=6;i<this.day.length();i++)
			this.yy = ""+this.day.charAt(i);

	}

	public void uppdater() {
		setChanged();
		notifyObservers();
	}

	public void insertEventoOnDb() {
		this.evn_id = this.get_LastEvnIDFromDB();
		String Query ="INSERT INTO evento (evn_id,nome,descrizione,luogo,day,time,level,type,acc_id) " +
				"VALUES ("+this.evn_id +",'" +this.nome +
				"','" + this.descr +
				"','" + this.luogo +
				"','" + this.day +
				"','" + this.time +
				"'," + this.level +
				",'" + this.type +
				"'," + Database.Utente.getID() +
				")";
		Database.setResource(Query);
	}

	public void addInvitato(String acc_id) {
		Database.setResource("INSERT INTO invitati (evn_id,inv_id) VALUES ("+ this.evn_id +","+ acc_id +")");
		new Notifica(0,acc_id,this.day);
		this.setInvitati(Database.evnInv(this.evn_id));
	}

	public void removeInvitato(String acc_id) {
		Database.deleteResource("DELETE FROM invitati WHERE acc_id="+this.evn_id+" AND cnt_id="+acc_id);
		this.setInvitati(Database.evnInv(this.evn_id));
	}

	public void setDataCal(Date date) {
		this.data = date;
	}

	public Date getDataCal() {
		return this.data ;
	}

	public Vector<Account> getInvitati() {
		return invitati;
	}

	public void setInvitati(Vector<Account> invitati) {
		this.invitati = invitati;
	}

	public static void updateEventOnDb(int id){
		Vector<Integer> oldInvitati = SearchEvent.getInvitatiOfEvent(id);
		boolean flag=false;
		Database.setResource("UPDATE evento SET  nome ='"+ Database.tempEvento.getNome() +
				"',luogo ='"+Database.tempEvento.getLuogo() +
				"',descrizione = '" + Database.tempEvento.getDescr() +
				"',day = '"+Database.tempEvento.getDay()+
				"',time = '"+Database.tempEvento.getTime() +
				"',level = "+Database.tempEvento.getLevel()+
				",type = '"+Database.tempEvento.getType()+
				"' WHERE evn_id ="+Database.tempEvento.getEvn_id());
		for (int i=0;i<oldInvitati.size();i++){
			for (int j=0;j<ModifyEvent.invitato.size();j++)
				if (oldInvitati.get(i)==ModifyEvent.invitato.get(j)){
					new Notifica(2,String.valueOf(Database.Utente.getID()),"");
					flag=false;
					break;
				}
				else
					flag = true;
			if (flag){
				Database.deleteResource("DELETE FROM invitati WHERE evn_id = " + ModifyEvent.Evento.getEvn_id() + " AND inv_id = "+oldInvitati.get(i));
				new Notifica(3,String.valueOf(Database.Utente.getID()),"");
			}
		}
		flag=false;
		for (int j=0;j<ModifyEvent.invitato.size();j++){
			for (int i=0;i<oldInvitati.size();i++)
				if (ModifyEvent.invitato.get(j)==oldInvitati.get(i)){
					flag=false;
					break;
				}
				else
					flag=true;
			if (flag){
				Database.setResource("INSERT INTO invitati (evn_id,inv_id) "+
						"VALUES ("+ModifyEvent.Evento.getEvn_id() +
						"," +	ModifyEvent.invitato +
						")");
				new Notifica(0,String.valueOf(Database.Utente.getID()),ModifyEvent.Evento.day);
			}
		}

	}

	public static void delEvent(int id) {
		Vector<Integer> invitati = SearchEvent.getInvitatiOfEvent(id);
		for (int i=0;i<invitati.size();i++){
			Database.deleteResource("DELETE FROM invitati WHERE evn_id = " + id + " AND inv_id = " + invitati.get(i));
			new Notifica(3,String.valueOf(Database.Utente.getID()),Database.Evento.getNome());
		}
		Database.deleteResource("DELETE FROM evento WHERE evn_id = " + id);

	}
}

